﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
using Yitter.IdGenerator;

namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsfooddesktagServices : Repository<corecmsfooddesktag>, IcorecmsfooddesktagServices
    {
        public bool InsertAsync(corecmsfooddesktag entity)
        {
            return base.Change<corecmsfooddesktag>().Insert(entity);
        }

        public bool UpdateDeskTag(long deskId)
        {
            corecmsfooddesktag entity = new corecmsfooddesktag() { deskId = deskId , modifyTime = YitIdHelper.NextId() };
            List<corecmsfooddesktag> list = new List<corecmsfooddesktag>
            {
                entity
            };
            return base.Change<corecmsfooddesktag>().InsertOrUpdate(list);
        }

        public bool InsertOrUpdate(List<corecmsfooddesktag> entity)
        {
            return base.InsertOrUpdate(entity);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsfooddesktag entity)
        {
            return base.Change<corecmsfooddesktag>().Update(a => new corecmsfooddesktag()
            {                
                modifyTime = entity.modifyTime,
            }, a => a.deskId == entity.deskId);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsfooddesktag> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsfooddesktag>().Update(a => new corecmsfooddesktag()
                {
                    modifyTime = item.modifyTime,
                }, a => a.deskId == item.deskId);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsfooddesktag>().DeleteById(id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsfooddesktag>().DeleteById(id);
            }
            return returnKey;
        }

        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsfooddesktag> GetCaChe()
        {
            return base.GetList();
        }

        #endregion
                
        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfooddesktag> QueryAsync(long lastId)
        {
            List<corecmsfooddesktag> list = new List<corecmsfooddesktag>();
            var where = Expressionable.Create<corecmsfooddesktag>();
            if (lastId > 0)
            {
                where = where.And(a => a.modifyTime > lastId);
            }
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.deskId).ToList();
            return list;
        }
        #endregion

        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<long> QueryDeskIdsAsync(long lastId)
        {
            List<corecmsfooddesktag> list = new List<corecmsfooddesktag>();
            var where = Expressionable.Create<corecmsfooddesktag>();
            where = where.And(a => a.modifyTime > lastId);
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.deskId).ToList();
            return list.Select(p=>p.modifyTime).ToList();
        }
        #endregion
    }
}
